
namespace Core {
    export class EasyLoading extends BaseClass {
        private content: Laya.Sprite = null;
        private speed: number = 10 / (1000 / 60);
        private averageUtils: AverageUtils;
        private uiImageContainer: Laya.Sprite;
        private imgUrl: string = "resource/assets/load_Reel.png"

        constructor() {
            super();
            this.init();
        }

        private init(): void {
            this.averageUtils = new AverageUtils();

            this.content = new Laya.Sprite();
            this.content.graphics.alpha(0.2)
            this.content.graphics.drawRect(0, 0, App.StageUtils.getWidth(), App.StageUtils.getHeight(), "#0x000000");
            //this.content.touchEnabled = true;

            this.uiImageContainer = new Laya.Sprite()
            this.uiImageContainer.x = this.content.width * 0.5;
            this.uiImageContainer.y = this.content.height * 0.5;
            this.uiImageContainer.loadImage(this.imgUrl)
            //TODO 可能有点歪,以后调整
            this.content.addChild(this.uiImageContainer);
        }

        public showLoading(): void {
            App.StageUtils.getStage().addChild(this.content);
            App.TimerManager.doFrame(1, 0, this.enterFrame, this);
        }

        public hideLoading(): void {
            if (this.content && this.content.parent) {
                App.StageUtils.getStage().removeChild(this.content);
                this.uiImageContainer.rotation = 0;
            }
            App.TimerManager.remove(this.enterFrame, this);
        }

        private enterFrame(time: number) {
            this.averageUtils.push(this.speed * time);
            this.uiImageContainer.rotation += this.averageUtils.getValue();
        }
    }
}